var express = require('express');
var router = express.Router();

const sql = require("../sql/sql");
const tools = require("../tools");
const order = require("../sql/order");
const cart = require("../sql/cart");

function createId(){
    return Date.now() + "" + parseInt(Math.random()*10000);
}


/**
* @apiGroup 订单
* @api {post} /api/order/add 添加订单
* @apiVersion 0.0.0
* @apiDescription 根据用户id和购物车数据，添加订单
* @apiParam {String} userId 用户Id
* @apiParam {String} proData 订单商品详情
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"请求成功",
* }
* @apiSampleRequest http://localhost:3000/api/order/add
*/

// 添加订单 接口
router.post('/add', function(req, res, next) {
    const {userId, proData} = req.body;
    const token = req.headers.token;

    tools.verify(token).then(()=>{
        // 插入订单数据
        sql.insert(order, {
            orderId: createId(),
            userId: userId,
            proData:proData,
            time:Date.now(),
            state:0
        }).then(()=>{
            // 删除购物车中的选中的数据
            // 先获取购物车所有数据
            sql.find(cart, {userId}).then(data=>{
                let arr = data[0].proData
                // 找到被选中的数据，剔除掉
                arr = arr.filter(val=>{
                    return !val.flag
                })
                // 更新购物车的数据
                sql.update(cart,{userId},{
                    proData:arr
                }).then(()=>{
                    res.send({
                        state:200,
                        title:"添加待付款订单成功"
                    })
                })
            })
        })
    }).catch(err=>{
        res.send({
            state:10086,
            title:"登录失效，重新登录"
        })
    })
});




/**
* @apiGroup 订单
* @api {get} /api/order 获取订单
* @apiVersion 0.0.0
* @apiDescription 根据用户id，获取订单
* @apiParam {String} userId 用户Id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {Array} data 请求说明
* @apiSuccessExample {json} Success-Response:
res.send({
    state:200,
    title:"获取成功",
    data:[{},{},{},...]
})
* @apiSampleRequest http://localhost:3000/api/order
*/

// 获取订单 接口
router.get('/', function(req, res, next) {
    const {userId} = req.query;

    const token = req.headers.token;

    tools.verify(token).then(()=>{
        sql.find(order,{userId},{_id:0,__v:0}).then(data=>{
            res.send({
                state:200,
                title:"获取成功",
                data
            })
        })
    }).catch(err=>{
        res.send({
            state:10086,
            title:"登录失效，重新登录"
        })
    })
});




/**
* @apiGroup 订单
* @api {post} /api/order/pay 修改支付状态
* @apiVersion 0.0.0
* @apiDescription 根据用户id 和 订单id，修改支付状态
* @apiParam {String} userId 用户Id
* @apiParam {String} orderId 订单Id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
res.send({
    state:200,
    title:"修改成功"
})
* @apiSampleRequest http://localhost:3000/api/order/pay
*/

router.post('/pay', function(req, res, next) {
    const {userId, orderId} = req.body;

    const token = req.headers.token;

    tools.verify(token).then(()=>{
        sql.update(order,{userId,orderId},{
            state: 1
        }).then(()=>{
            res.send({
                state:200,
                title:"修改成功"
            })
        })
    }).catch(err=>{
        res.send({
            state:10086,
            title:"登录失效，重新登录"
        })
    })
});




/**
* @apiGroup 订单
* @api {post} /api/order/detail 获取订单详情
* @apiVersion 0.0.0
* @apiDescription 根据用户id 和 订单id，获取订单详情
* @apiParam {String} userId 用户Id
* @apiParam {String} orderId 订单Id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {Object} data 请求数据
* @apiSuccessExample {json} Success-Response:
res.send({
    state:200,
    title:"修改成功",
    data:{}
})
* @apiSampleRequest http://localhost:3000/api/order/detail
*/

router.get('/detail', function(req, res, next) {
    const {userId, orderId} = req.query;

    const token = req.headers.token;

    tools.verify(token).then(()=>{
        sql.find(order,{userId,orderId},{_id:0,__v:0}).then((data)=>{
            res.send({
                state:200,
                title:"获取详情成功",
                data:data[0]
            })
        })
    }).catch(err=>{
        res.send({
            state:10086,
            title:"登录失效，重新登录"
        })
    })
});

module.exports = router;
